//
//  ___FILENAME___
//  ___PACKAGENAME___
//
//  Created by ___FULLUSERNAME___ on ___DATE___.
//  Copyright (c) ___YEAR___ ___ORGANIZATIONNAME___. All rights reserved.
//

#import "GLViewController.h"
#import "ConstantsAndMacros.h"
#import "OpenGLCommon.h"

@implementation GLViewController

- (void)loadView
{
    GLView *viewGL = [[GLView alloc] init];
    viewGL.delegate = self;
    self.view = viewGL;
    [viewGL release];
	
	[viewGL startAnimation];
}


- (void)drawCube {
	
	static float rot = 0.0;
	static const ColoredVertexData3D vertexData[] = {
		{
			{0, -0.525731, 0.850651},           // Vertex |
			{0.000000, -0.417775, 0.675974},    // Normal | Vertex 0
			{1.0, 0.0, 0.0, 1.0}                // Color  |
		},
		{
			{0.850651, 0, 0.525731},            // Vertex |
			{0.675973, 0.000000, 0.417775},     // Normal | Vertex 1
			{1.0, 0.5, 0.0, 1.0}                // Color  |
		},
		{
			{0.850651, 0, -0.525731},           // Vertex |
			{0.675973, -0.000000, -0.417775},   // Normal | Vertex 2
			{1.0, 1.0, 0.0, 1.0}                // Color  |
		},
		{
			{-0.850651, 0, -0.525731},          // Vertex |
			{-0.675973, 0.000000, -0.417775},   // Normal | Vertex 3
			{0.5, 1.0, 0.0, 1.0}                // Color  |
		},
		{
			{-0.850651, 0, 0.525731},           // Vertex |
			{-0.675973, -0.000000, 0.417775},   // Normal | Vertex 4
			{0.0, 1.0, 0.0, 1.0}                // Color  |
		},
		{
			{-0.525731, 0.850651, 0},           // Vertex |
			{-0.417775, 0.675974, 0.000000},    // Normal | Vertex 5
			{0.0, 1.0, 0.5, 1.0}                // Color  |
		},
		{
			{0.525731, 0.850651, 0},            // Vertex |
			{0.417775, 0.675973, -0.000000},    // Normal | Vertex 6
			{0.0, 1.0, 1.0, 1.0}                // Color  |
		},
		{
			{0.525731, -0.850651, 0},           // Vertex |
			{0.417775, -0.675974, 0.000000},    // Normal | Vertex 7
			{0.0, 0.5, 1.0, 1.0}                // Color  |
		},
		{
			{-0.525731, -0.850651, 0},          // Vertex |
			{-0.417775, -0.675974, 0.000000},   // Normal | Vertex 8
			{0.0, 0.0, 1.0, 1.0},               // Color  |
		},
		{
			{0, -0.525731, -0.850651},          // Vertex |
			{0.000000, -0.417775, -0.675973},   // Normal | Vertex 9
			{0.5, 0.0, 1.0, 1.0}                // Color  |
		},
		{
			{0, 0.525731, -0.850651},           // Vertex |
			{0.000000, 0.417775, -0.675974},    // Normal | Vertex 10
			{1.0, 0.0, 1.0, 1.0}                // Color  |
		},
		{
			{0, 0.525731, 0.850651},            // Vertex |
			{0.000000, 0.417775, 0.675973},     // Normal | Vertex 11
			{1.0, 0.0, 0.5, 1.0}                // Color  |
		}
	};

	
	static const GLubyte icosahedronFaces[] = {
        1, 2, 6,
        1, 7, 2,
        3, 4, 5,
        4, 3, 8,
        6, 5, 11,
        5, 6, 10,
        9, 10, 2,
        10, 9, 3,
        7, 8, 9,
        8, 7, 0,
        11, 0, 1,
        0, 11, 4,
        6, 2, 10,
        1, 6, 11,
        3, 5, 10,
        5, 4, 11,
        2, 7, 9,
        7, 1, 0,
        3, 9, 8,
        4, 8, 0,
    };

	
	glLoadIdentity();
    glTranslatef(0.0f,0.0f,-3.0f);
    glRotatef(rot,1.0f,1.0f,1.0f);
	
    glClearColor(0.7, 0.7, 0.7, 1.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);
	glEnableClientState(GL_NORMAL_ARRAY);
	glEnable(GL_COLOR_MATERIAL);
	
	//int vertexCount = sizeof(vertexData)/sizeof(ColoredVertexData3D);
	glVertexPointer(3, GL_FLOAT, sizeof(ColoredVertexData3D), &vertexData[0].vertex); //指定下一个数据,间隔多少字节
	glColorPointer(4, GL_FLOAT, sizeof(ColoredVertexData3D), &vertexData[0].color);
	glNormalPointer(GL_FLOAT, sizeof(ColoredVertexData3D), &vertexData[0].normal);

	//glDrawArrays(GL_TRIANGLES, 0, 12); //不用icosahedronFaces的方式: 最多12个点画，可以画出4个三角形
	
	glDrawElements(GL_TRIANGLES, 60, GL_UNSIGNED_BYTE, icosahedronFaces);
	
    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_COLOR_ARRAY);
	glDisableClientState(GL_NORMAL_ARRAY);
	glDisable(GL_COLOR_MATERIAL);
	
    static NSTimeInterval lastDrawTime;
    if (lastDrawTime)
    {
		NSTimeInterval timeSinceLastDraw = [NSDate timeIntervalSinceReferenceDate] - lastDrawTime;
        rot += 50 * timeSinceLastDraw;
	}
	
    lastDrawTime = [NSDate timeIntervalSinceReferenceDate];
}


#pragma mark - GLViewDelegate

- (void)drawView:(UIView *)theView
{
	[self drawCube];
}

#pragma mark - Setup View

-(void)setupView:(GLView*)view
{
	const GLfloat zNear = 0.01, zFar = 1000.0, fieldOfView = 45.0;
    GLfloat size;
    glEnable(GL_DEPTH_TEST);
	
    glMatrixMode(GL_PROJECTION);
    size = zNear * tanf(DEGREES_TO_RADIANS(fieldOfView) / 2.0);
    CGRect rect = view.bounds;
    glFrustumf(-size, size, -size / (rect.size.width / rect.size.height), size /
               (rect.size.width / rect.size.height), zNear, zFar);
    glViewport(0, 0, rect.size.width, rect.size.height);
	
    glMatrixMode(GL_MODELVIEW);
			
    glLoadIdentity();
}

@end
